﻿using System;
using System.Collections.ObjectModel;
using System.Data.SQLite;
using System.Globalization;
using WpfApp1.Model;

namespace WpfApp1.ViewModel
{
    class TripsViewModel
    {
        private ObservableCollection<TripModel> observableCollection = new ObservableCollection<TripModel>();

        public ObservableCollection<TripModel> ObservableCollection { get => observableCollection; set => observableCollection = value; }

        public TripsViewModel()
        {
            string sql_create = "CREATE TABLE if not exists trip ( id integer PRIMARY KEY AUTOINCREMENT,title nvarchar,start_time navarchar,end_time nvarchar,area nvarchar,remark nvarchar); ";
            SQLiteCommand command = new SQLiteCommand(sql_create, MainWindow.connecttion);
            command.ExecuteNonQuery();
            string sql_list = "select * from trip";
            command = new SQLiteCommand(sql_list, MainWindow.connecttion);
            SQLiteDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                long id = Convert.ToInt64(reader["id"]);
                string title = reader["title"].ToString();
                string start = reader["start_time"].ToString();
                string end = reader["end_time"].ToString();
                string area = reader["area"].ToString();
                string car = reader["car_cost"].ToString();
                string hotel = reader["hotel_cost"].ToString();
                string total = reader["total_cost"].ToString();
                string remark = reader["remark"].ToString();

                if (!Tools.ValidData(start) && !Tools.ValidData(end))
                {
                    continue;
                }

                DateTime startTime = Convert.ToDateTime(start);
                DateTime endTime = Convert.ToDateTime(end);
                observableCollection.Add(new TripModel()
                {
                    Id = id,
                    Title = title,
                    Start_time = startTime,
                    End_time = endTime,
                    Area = area,
                    Car_cost = car,
                    Hotel_cost = hotel,
                    Total_cost = total,
                    Remark = remark
                });
            }
        }
    }
}
